/** Batch represents a page modified by LU JINBO@10-10-14**/

package qp.utils;

import java.util.Vector;
import java.io.Serializable;

public class Batch implements Serializable{

    int MAX_SIZE;  // Number of tuples per page
    static int PageSize;  /* Number of bytes per page**/

    Vector<Tuple> tuples; // The tuples in the page


	/** Set number of bytes per page **/
	public static void setPageSize(int size){
		PageSize=size;
	}

	/** get number of bytes per page **/
	public static int getPageSize(){
		return PageSize;
	}

	/** Number of tuples per page **/

    public Batch(int numtuple){
		MAX_SIZE=numtuple;
		tuples = new Vector(MAX_SIZE);
    }


    /** insert the record in page at next free location**/

    public void add(Tuple t){
	tuples.add(t);
    }

    public int capacity(){
	return MAX_SIZE;

    }

    
	public void sort(int index) {
		int min=0;
		int compareFlag;
		for(int i=0; i<size()-1;i++) {
			for(int j=i+1;j<size();j++) {
				compareFlag=Tuple.compareTuples(tuples.elementAt(min), tuples.elementAt(j),index);
				 if(compareFlag>0) {
					   min=j;
				   }		
			}
			if(min!=i) {
				Tuple tempTuple=tuples.remove(i);
				tuples.insertElementAt(tuples.elementAt(min-1),i);
				tuples.remove(min);
				tuples.insertElementAt(tempTuple, min);
			}
			min=i+1;
		}
	}
	//sort the Batch on index
	public void sortDescendent(int index) {
		int max=0;
		int compareFlag;
		for(int i=0; i<size()-1;i++) {
			for(int j=i+1;j<size();j++) {
				compareFlag=Tuple.compareTuples(tuples.elementAt(max), tuples.elementAt(j),index);
				 if(compareFlag<0) {
					   max=j;
				   }		
			}
			if(max!=i) {
				Tuple tempTuple=tuples.remove(i);
				tuples.insertElementAt(tuples.elementAt(max-1),i);
				tuples.remove(max);
				tuples.insertElementAt(tempTuple, max);
			}
			max=i+1;
		}
	}
	
	
	//sort the Batch on Mulindex********added by ZJX
	public void sortMulAttri(Vector<Integer> index,Vector<String> ascORdesc) {
		int pick=0;
		int compareFlag;
		for(int i=0; i<size()-1;i++) {
			for(int j=i+1;j<size();j++) {
				
				boolean find=false;
				for(int f=0;f<ascORdesc.size()&&(!find);f++)
				{
					compareFlag=Tuple.compareTuples(tuples.elementAt(pick), tuples.elementAt(j),index.elementAt(f));
					if(ascORdesc.elementAt(f).equalsIgnoreCase("asc"))
					{
						if(compareFlag<0)//J > pick
						{
							find=true;
						}//no action
						else if(compareFlag>0)//j<pick
						{
							pick=j;
						    find=true;
						}
						//else compareFlag==0
					}
					if(ascORdesc.elementAt(f).equalsIgnoreCase("desc"))
					{
						if(compareFlag<0)//J > pick
						{
							pick=j;
							find=true;
						}//no action
						else if(compareFlag>0)//j<pick
						{
						    find=true;
						}
						//else compareFlag==0
					}
					
					
				}
					
			}
			if(pick!=i) {
				Tuple tempTuple=tuples.remove(i);
				tuples.insertElementAt(tuples.elementAt(pick-1),i);
				tuples.remove(pick);
				tuples.insertElementAt(tempTuple, pick);
			}
			pick=i+1;
		}
	}
	
	
    public void clear(){
	tuples.clear();
    }

    public boolean contains(Tuple t){
	return tuples.contains(t);
    }

    public Tuple elementAt(int i){
	return (Tuple) tuples.elementAt(i);
    }

    public int indexOf(Tuple t){
	return tuples.indexOf(t);
    }

    public void insertElementAt(Tuple t, int i){
	tuples.insertElementAt(t,i);
    }

    public boolean isEmpty(){
	return tuples.isEmpty();
    }

    public void remove(int i){
	tuples.remove(i);
    }

    public Tuple remove(){
	return (Tuple) tuples.remove(0);
    }
    public Tuple remove_z(int i){
	return (Tuple)  tuples.remove(i);
    }

    
    public void clean()
   	{
    		tuples.removeAllElements(); 
   	}
	
    public void setElementAt(Tuple t, int i){
	tuples.setElementAt(t,i);
    }

    public int size(){
	return tuples.size();
    }

    public Vector<Tuple> getTuples(){
	return tuples;
    }
    public boolean isFull(){
	if(size() == capacity())
	    return true;
	else
	    return false;
    }
}
